<?php

namespace console\controllers;

use yii\console\Controller;
use common\components\UpdateCache;
use linslin\yii2\curl\Curl;

/**
 * 调用node服务推送信息
 * 
 * yii "model class name"
 * 
 * @author kai.gao
 *
 */
class NodeController extends Controller {
	/**
	 * 
	 * @param unknown $id
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2015-5-26下午4:46:36
	 * @modified_date: 2015-5-26下午4:46:36
	 * @modified_user: kai.gao
	 */
	public function actionIndex($type, $message, $message_type = 'console', $id = '') {
		$curl = new Curl();
		echo 'start....';
		$key = '51dh_node';
		$url = 'http://'.NODE_URL.'/send';
// 		$url = 'http://web.51dh.com/node/http-post';
// 		echo $url;
		echo $this->http_post($url, [
			'token' => md5($key.date('Y-m-d', time())), 
			'type' => $type,
			'id' => $id,
			'message_type' => $message_type,
			'message' => $message
		]);
	}
	
	public function actionRedis() {
		list($a, $b) = explode(' ', microtime());
		$start = $a+$b;  
		for ($i=1; $i<2; $i++) {
// // 			f_c($i, 'fffffffffffffffffffffffffffffffff');
// 			f_c($i);
			$ck = '51dh_cache_key1';
			$ck = false ? $ck.'mob' : $ck.'pc';
			$index = \Yii::$app->page_cache->get($ck);
		}
		list($a, $b) = explode(' ', microtime());
		$end = $a+$b;
		
		echo $end - $start;
	}
	
	protected function http_post($uri, $data) {
		$ch = curl_init ();
		$data = http_build_query($data);
		curl_setopt($ch, CURLOPT_URL, $uri);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		$return = curl_exec ($ch);
		curl_close ($ch);
		return $return;
	}
}	

?>